package com.shensi.adminCollect.service.coupon.impl;

import com.alibaba.fastjson.JSONObject;
import com.shensi.adminCollect.common.enums.WxConsumptionMessageEnum;
import com.shensi.adminCollect.common.utils.DateUtils;
import com.shensi.adminCollect.domain.coupon.CouponCreation;
import com.shensi.adminCollect.domain.coupon.CouponInfo;
import com.shensi.adminCollect.domain.coupon.cancel.CancelCouponDto;
import com.shensi.adminCollect.domain.coupon.cancel.CancelCouponTemplate;
import com.shensi.adminCollect.domain.coupon.cancel.CancelCouponUser;
import com.shensi.adminCollect.mapper.coupon.CouponInfoMapper;
import com.shensi.adminCollect.service.coupon.ICouponInfoService;
import com.shensi.api.pay.client.RedisConsumptionThemeClient;
import com.shensi.common.core.domain.UniversalRedisMessageDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 优惠劵Service业务层处理
 * 
 * @author qujia
 * @date 2021-08-09
 */

@Slf4j
@Service
public class CouponInfoServiceImpl implements ICouponInfoService 
{
    @Autowired
    private CouponInfoMapper couponInfoMapper;

    @Autowired
    private RedisTemplate redisTemplate;

    @Value("${message.channel}")
    private String channel;

    @Autowired
    private RedisConsumptionThemeClient redisConsumptionThemeClient;

    /**
     * 查询优惠劵
     * 
     * @param id 优惠劵ID
     * @return 优惠劵
     */
    @Override
    public CouponInfo selectCouponInfoById(Integer id)
    {
        return couponInfoMapper.selectCouponInfoById(id);
    }

    /**
     * 查询优惠劵列表
     *
     * @param couponInfo 优惠劵
     * @return 优惠劵
     */
    @Override
    public List<CouponInfo> selectCouponInfoList(CouponInfo couponInfo)
    {
        return couponInfoMapper.selectCouponInfoList(couponInfo);
    }


//    /**
//     * 查询优惠劵列表
//     *
//     * @return 优惠劵
//     */
//    @Override
//    public List<CouponInfo> selectCoupon(String date)
//    {
//        return couponInfoMapper.selectCoupon(date);
//    }

    /**
     * 新增优惠劵
     * 
     * @param couponInfo 优惠劵
     * @return 结果
     */
    @Override
    public int insertCouponInfo(CouponInfo couponInfo)
    {
        couponInfo.setCreateTime(DateUtils.getNowDate());
        return couponInfoMapper.insertCouponInfo(couponInfo);
    }

    /**
     * 创建优惠劵
     *
     * @param couponCreation 优惠劵
     * @return 结果
     */
    @Override
    public int insertCouponCreation(CouponCreation couponCreation)
    {
        couponCreation.setCreateTime(DateUtils.getNowDate());
        return couponInfoMapper.insertCouponCreation(couponCreation);
    }

    /**
     * 修改优惠劵
     * 
     * @param couponInfo 优惠劵
     * @return 结果
     */
    @Override
    public int updateCouponInfo(CouponInfo couponInfo)
    {
        couponInfo.setUpdateTime(DateUtils.getNowDate());
        return couponInfoMapper.updateCouponInfo(couponInfo);
    }

    /**
     * 修改优惠劵
     *
     * @param couponCreation 优惠劵
     * @return 结果
     */
    @Override
    public int updateCouponCreation(CouponCreation couponCreation)
    {
        couponCreation.setUpdateTime(DateUtils.getNowDate());
        return couponInfoMapper.updateCouponCreation(couponCreation);
    }

    /**
     * 批量删除优惠劵
     * 
     * @param ids 需要删除的优惠劵ID
     * @return 结果
     */
    @Override
    public int deleteCouponInfoByIds(Integer[] ids)
    {
        return couponInfoMapper.deleteCouponInfoByIds(ids);
    }

    /**
     * 批量删除优惠劵
     *
     * @param ids 需要删除的优惠劵ID
     * @return 结果
     */
    @Override
    public int deleteCouponInfoByCouponIds(Integer[] ids)
    {
        return couponInfoMapper.deleteCouponInfoByCouponIds(ids);
    }

    /**
     * 删除优惠劵信息
     * 
     * @param id 优惠劵ID
     * @return 结果
     */
    @Override
    public int deleteCouponInfoById(Integer id)
    {
        return couponInfoMapper.deleteCouponInfoById(id);
    }

    @Override
    public void cancelCoupon(CancelCouponDto cancelCouponDto) {

        UniversalRedisMessageDto dto=new UniversalRedisMessageDto();
        dto.setData(JSONObject.toJSONString(cancelCouponDto));
        dto.setMessageType(WxConsumptionMessageEnum.OIL_PRICE_ADJUSTMENT_PUSH.getValue());
        dto.setTitle("核销优惠券");
        dto.setChannel(channel);
        //String str=JSONObject.toJSONString(dto);
        //redisTemplate.convertAndSend(channel, dto);
        redisConsumptionThemeClient.publishMessageUse(dto);
    }

    @Override
    public List<CancelCouponUser> selectUserAllCoupons(CancelCouponUser couponUser) {
        return couponInfoMapper.selectUserAllCoupons(couponUser);
    }

    @Override
    public int updateCouponUser(CancelCouponUser cancelCouponUser) {
        return couponInfoMapper.updateCouponUser(cancelCouponUser);
    }

    @Override
    public int updateCouponTemplate(CancelCouponTemplate cancelCouponTemplate) {
       return couponInfoMapper.updateCouponTemplate(cancelCouponTemplate);
    }

    @Override
    public int incrGetCouponNum(Integer id, int incr) {
        return couponInfoMapper.incrGetCouponNum(id,incr);
    }
}
